var app = getApp()
Page({
    data: {
        region: ['请选择', '请选择', '请选择'],
        customItem: '全部',
        name: '',
        phone: '',
        area: '',
        address: '',

        isChecked: false,
        status: 0,
        is_submit: false, // 是否提交

        address_type: "", // 地址类型
    },
    // formid
    formSubmit: function(e) {
        var formId = e.detail.formId;
        var that = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/user/collect_formid',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        formid: formId
                    },
                    success: function(res) {
                        if (res.data.code == 1) {

                        }
                    }
                })
            }
        })
    },

    // 选择地址
    bindRegionChange(e) {
        this.setData({
            area: e.detail.value.join(''),
            region: e.detail.value
        })
    },
    // 姓名赋值
    valName(e) {
        this.setData({
            name: e.detail.value
        })
    },
    // 手机号赋值
    valPhon(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    // 详细地址赋值
    valAddress(e) {
        this.setData({
            address: e.detail.value
        })
    },
    // 设置是否为默认地址
    changeSwitch: function(e) {
        this.setData({
            isChecked: e.detail.value
        })
        if (this.data.isChecked == false) {
            this.setData({
                status: 0
            })
        } else {
            this.setData({
                status: 1
            })
        }
    },
    onLoad: function(options) {
        this.setData({
            address_type: options.type ? options.type : "",
        })
    },
    address: function() {
        var that = this;
        var address_type = this.data.address_type;
        var is_submit = this.data.is_submit;

        var data = {};
        data.name = this.data.name;
        data.phone = this.data.phone;
        data.area = this.data.area;
        data.address = this.data.address;
        data.status = this.data.status;

        if (!data.name) { wx.showToast({ image: "../../img/warning.png", title: "请输入姓名", }); return false; }
        if (!data.phone) { wx.showToast({ image: "../../img/warning.png", title: "请输入手机号", }); return false; }
        if (!data.area) { wx.showToast({ image: "../../img/warning.png", title: "请选择地址", }); return false; }
        if (!data.address) { wx.showToast({ image: "../../img/warning.png", title: "请输入详细地址", }); return false; }

        if (!is_submit) {
            this.setData({is_submit: true})

            wx.getStorage({
                key: 'user',
                success: function(res) {
                    data.openid = res.data.openid;
                    data.token = res.data.token;

                    wx.request({
                        url: app.url.requestUrl + '/personal/addressAdd',
                        method: 'POST',
                        data,
                        success: function(res) {
                            if (res.data.code == 1) {
                                wx.showToast({
                                    title: res.data.msg
                                })
                                setTimeout(function(){
                                    if (data.status && address_type == 'select') {
                                        wx.navigateBack({
                                            delta: 2
                                        })
                                    } else {
                                        wx.navigateBack({
                                            delta: 1
                                        })
                                    }
                                },1500)
                            } else {
                                that.setData({is_submit: false})

                                wx.showToast({
                                    image: "../../img/warning.png",
                                    title: res.data.msg,
                                })
                            }
                        }
                    })
                }
            })
        }

    }
})